ChatGPT Prompt Engineering

Abstract

Prompt Engineering 是生成式大语言模型出现后的新兴技术,以 ChatGPT 为代表的 AI 软件,在特定的 Prompt 下能更容易得到期望的结果。本文章主要基于 OpenAI 官方与吴恩达合作的系列 教学课程

Prompt guidelines

  • Be clear and specific
  • Analyze why result does not give desired output
  • Refine the idea and the prompt
  • Repeat

Code Guidances

Delimiters

text = f"""
Prompt Engineering 是生成式大语言模型出现后的新兴技术,以 ChatGPT 为代表的 AI 软件,在特定的 Prompt 下能更容易得到期望的结果。
"""
# 将 text 嵌入 prompt 中
prompt = f"""
Summarize the text delimited by triple backticks into a single sentence. \ 
```{text}```
""" 

Iterative

Temperature

Get completions

def get_completions(prompt, model="gpt-3.5-turbo"):
	messages = [{"role": "user", "content", prompt}]
	response = openai.ChatCompletion.create(
		model = model,
		messages = messages,
		temperature = 0,	
	)
	return response.choices[0].message["content"]

Capabilities

Summarize

# 使用 extract 和 limit
prompt = f"""
Your task is to extract relevant information from \
a product review from an ecommerce site to give \
feedback to the Shipping department.

From the review below, delimited by triple quotes \
extract the information relevant to shipping and \
delivery. Limit to 30 words.

Review: ```{prod_review}```
"""

Inferring

topic_list = [
    "nasa", "local government", "engineering", 
    "employee satisfaction", "federal government"
]

prompt = f"""
Determine whether each item in the following list of \
topics is a topic in the text below, which \
is delimited with triple backticks.

Give your answer as list with 0 or 1 for each topic.
Please Format your response as a JSON object with \
topics as the keys.

Format your response as a JSON object.

List of topics: {", ".join(topic_list)}

Text sample: '''{story}'''
"""
response = get_completion(prompt)
print(response)

Transforming

Translation

prompt = f"""
Translate the following text to English: \
```{text}```
"""

Tone Transformation

prompt = f"""
Translate the following from slang to a business letter: 
```{text}```
"""

Spellcheck/Grammar check

prompt = f"proofread and correct this review: ```{text}```"

Expanding

prompt = f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given the customer email delimited by ```, \
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \
their review.
If the sentiment is negative, apologize and suggest that \
they can reach out to customer service. 
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sign the email as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: ```{sentiment}```

"""

Chatbot

def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature,
    )
    return response.choices[0].message["content"]

def ask_chatbot():
	context = []
	role = input("Set the role of the chatbot: \n")
	context.append({'role':'system', 'content': f"{role}"})
	while True:
		prompt = input('You: ')
		context.append({'role': 'user', 'content': f"{prompt}"})
		response = get_completion_from_messages(context) 
		print("GPT: {}".format(response))
		context.append({'role': 'assistant', 'content': f"{response}"})